package com.android.dialer.extendboard;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import android.support.annotation.NonNull;

public class SpeedDialItem implements Parcelable {
    private static final String TAG = "SpeedDialItem";
    private int id;
    private String avatar;
    private String name;
    private String number;
    private boolean isActive;

    public SpeedDialItem() {
        id = -1;
        name = "";
        number = "";
        avatar = "";
        isActive = false;
    }

    public SpeedDialItem(int id, String iconOrName, String nameOrNumber, boolean isActive) {
        this.id = id;
        this.name = iconOrName;
        this.number = nameOrNumber;
        this.isActive = isActive;
        avatar = "";
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getAvatar() {
        return avatar;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }


    public void setNumber(String number) {
        this.number = number;
    }

    protected SpeedDialItem(Parcel in) {
        id = in.readInt();
        avatar = in.readString();
        name = in.readString();
        number = in.readString();
        isActive = in.readByte() != 0;
    }

    public static final Creator<SpeedDialItem> CREATOR = new Creator<SpeedDialItem>() {
        @Override
        public SpeedDialItem createFromParcel(Parcel in) {
            return new SpeedDialItem(in);
        }

        @Override
        public SpeedDialItem[] newArray(int size) {
            return new SpeedDialItem[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(avatar);
        dest.writeString(name);
        dest.writeString(number);
        dest.writeByte((byte) (isActive ? 1 : 0));
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
    }

    public byte[] toByteArray() {
        try {
            byte[] nameBytes = name.getBytes("UTF-8");
            byte[] numberBytes = number.getBytes("UTF-8");
            byte[] data = new byte[4 + nameBytes.length + numberBytes.length];

            Log.d(TAG, "toByteArray: number and name "+ number + " " + name);
            Log.d(TAG, "toByteArray: length of nameBytes: " + nameBytes.length);
            Log.d(TAG, "toByteArray: length of numberBytes: " + numberBytes.length);

            data[0] = (byte) id;
            data[1] = (byte) (isActive ? 1 : 0);
            data[2] = (byte) nameBytes.length;
            System.arraycopy(nameBytes, 0, data, 3, nameBytes.length);
            data[3 + nameBytes.length] = (byte) numberBytes.length;
            System.arraycopy(numberBytes, 0, data, 4 + nameBytes.length, numberBytes.length);

            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new byte[0];
    }

    @NonNull
    @Override
    public String toString() {
        return "SpeedDialItem{" +
                "id=" + id +
                ", avatar='" + avatar + '\'' +
                ", name='" + name + '\'' +
                ", number='" + number + '\'' +
                ", isActive=" + isActive +
                '}';
    }
}    